/*
 * {comment of class}
 *
 * @author (C) <a href="http://www.carfield.com.hk">Carfield Yim</a>, 2004
 */
package hk.com.carfield.action;

import hk.com.carfield.core.Context;
import hk.com.carfield.item.FileItem;
import hk.com.carfield.item.Item;
import hk.com.carfield.util.LangUtil;
import hk.com.carfield.util.LogUtil;
import hk.com.carfield.util.StringUtil;
import hk.com.carfield.util.UrlUtil;
import hk.com.carfield.web.Request;

import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

public final class Plain implements Body {
	public final static class PlainItem extends FileItem {
		private final String body;

		public PlainItem(final Context c, final File f, final String body)
				throws IOException {
			super(c, f, true);
			this.body = body;
		}

		@Override
		public String getCssClass() {
			return "head";
		}

		public String getDesc() {
			return body;
		}

		@Override
		public int getOrder() {
			return -2;
		}
	}

	public static final Map<String, AtomicInteger> notFoundFile = new ConcurrentHashMap<String, AtomicInteger>();
	final static private String stdMsg = "	Please try the following:"
			+ "<br/>	    * If you typed the page address in the Address bar, make sure that it is spelled correctly."
			+ "<br/>	    * Go to main page and look for links to the information you want."
			+ "<br/>	    * Use the search box to find the link you are looking for."
			+ "<br/>	    * Click the Back button to try another link.";

	public static Plain forbiddenBody(final File l, final Context c)
			throws IOException {
		final Plain p = new Plain();
		final String msg = "<b>An Error Occured (403)</b><br/>You are not authorized to access this document: "
				+ UrlUtil.getRelativePath(c.config, l);
		// TODO find closest files, or past file
		p.head = new PlainItem(c, l, msg + Plain.stdMsg);
		return p;
	}

	public static Plain getErrorBody(final Throwable t, final File l,
			final Context c, final Request req) {
		final Plain p = new Plain();
		try {
			p.head = new PlainItem(c, l, StringUtil.text2Html(new StringReader(
					t.getMessage())));
		} catch (final Exception e) {
			LogUtil.error(e, c.mailMan, l, req);
		}
		return p;
	}

	public static Plain notFoundBody(final File l, final Context c)
			throws IOException {
		final Plain p = new Plain();
		LangUtil.incrementMap(Plain.notFoundFile,
				UrlUtil.getRelativePath(c.config, l));
		final String msg = "<b>An Error Occured (404)</b><br/>The document you requested ("
				+ UrlUtil.getRelativePath(c.config, l)
				+ ") cannot be found. The document you are looking for might have been removed, had its name changed, or is temporarily unavailable.";
		// TODO find closest files, or past file
		p.head = new PlainItem(c, c.config.webRoot, msg + Plain.stdMsg);
		return p;
	}

	// TODO move text to bundle
	private Item head;

	private Plain() {
		// prevent initialize
	}

	public String actionCode() {
		return null;
	}

	public String getFoot() {
		return "";
	}

	public Item getHead() {
		return head;
	}

	public String getRedirectTo() {
		return null;
	}

	public void init(final Context c, final Request req, final File l) {
		// Just fulfill interface
	}
}